<?php
vendor('dBug');
import('@.Common.UCAdapter');
/****
 * 用户好友模块, 处理好友显示\添加\删除相关
****/
class FriendAction extends InitAction {
	protected function _initialize() {
        parent::_initialize();
	}
	public function index() {
		$uid= $this->getUID();
		$am= new AccountModel();
		$account= $am->find($uid);

		$this->assign('user', $account);

		$friends= $am->getFriends($uid);
		$this->assign('friends', $friends);

		$this->display();
	}
	public function rev() {
		$uid= $this->getUID();
		$am= new AccountModel();
		$account= $am->find($uid);

		$this->assign('user', $account);

		$friends= $am->getRevFriends($uid);
		$this->assign('friends', $friends);

		$this->display('index');		
	}
	public function add() {
		if (! array_key_exists('uid', $_GET) ) {
			$this->redirect('friend/');
			return;
		}
		$friendid= $_GET['uid'];
		$uid= $this->isLogged();
		if (! $uid) {
			$this->redirect('account/login', array(
				'from'=>urlencode(__SELF__)));
		}
		$am= new AccountModel();
		if ($am->hasFriend($uid, $friendid)) {
			// 不再重复添加好友
			if ($this->isAjax()) {
				$this->success("关注成功");
			} else {
				$this->redirect('friend/');
			}
			return ;
		}
		$r= UCAdapter::get()->friend_add($uid, $friendid);
		if (!$r) {
			$this->error("关注失败");
		} else {
			if ($this->isAjax()) {
				$this->success("关注成功");
			} else {
				$this->redirect('friend/');
			}
		}
	}
	public function del() {
		if (! array_key_exists('uid', $_GET) ) {
			$this->redirect('friend/');
			return;
		}
		$friendid= $_GET['uid'];
		$uid= $this->isLogged();
		if (! $uid) {
			$this->redirect('account/login', array(
				'from'=>urlencode(__SELF__)));
		}
		$r= UCAdapter::get()->friend_delete($uid, $friendid);
		if (!$r) {
			$this->error("取消失败");
		} else {
			if ($this->isAjax()) {
				$this->success("操作成功");
			} else {
				$this->redirect('friend/');
			}
		}
	}
	protected function getUID(){
		if (array_key_exists('uid', $_GET)) {
			$uid= $_GET['uid'];
		} else {
			$uid= $this->isLogged();
			if (! $uid) {
				$this->redirect('account/login', array(
					'from'=>urlencode(__SELF__)));
			}
		}
		return $uid;
	}
}
?>